﻿using Core.Shop.Model;
using Core.Shop.Model.DTO.BaseInfo;
using Core.Shop.Model.Entity;
using Core.Shop.Tool;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Core.Shop.Filter.AuthAttribute;

namespace Core.Shop.Api.BaseInfo
{
    public class RegisterController:BaseController
    {
        private PasswordHelper _passwordHelper;
        public RegisterController(PasswordHelper passwordHelper)
        {
            _passwordHelper = passwordHelper;
        }
        [NoPermissionRequired]
        [HttpPost]
        public object Post([FromBody] RegisterDTO request)
        {
            var user = Db.User.FirstOrDefault(m => m.UserName == request.UserName);
            if (user != null)
            {
                return ReturnModel.Fail("当前用户名已存在，请换一个用户名。");
            }
            if (!request.Password.Equals(request.ConfirmPassword))
            {
                return ReturnModel.Fail("两次密码输入不一致，请确认您的密码。");
            }
            var passwordSec = _passwordHelper.CodePassword(request.Password);
            user = new User()
            {
                NickName = request.NickName,
                UserName = request.UserName,
                Password = passwordSec,
            };
            Db.Add(user);
            var count = Db.SaveChanges();
            return ReturnModel.Success(count);


        }
    }
}
